<template>
    <div class="articleClass">
        <div class="crumbs">
            <el-breadcrumb separator="/">
                <el-breadcrumb-item><i class="el-icon-tickets"></i> 文章列表</el-breadcrumb-item>
            </el-breadcrumb>
        </div>
        <div class="container">
            <div class="handle-box">
                <el-button type="primary" icon="delete" class="handle-del mr10" @click="handleAdd">添加分类</el-button>
            </div>
            <el-table v-loading="loading"  element-loading-text="拼命加载中" :data="articleClassList" border style="width: 100%" ref="multipleTable" @selection-change="handleSelectionChange">
                <el-table-column type="index" width="50"></el-table-column>
                <el-table-column prop="createTime" label="日期" sortable >
                </el-table-column>
                <el-table-column prop="name" label="名称" >
                </el-table-column>
                <el-table-column label="操作" >
                    <template slot-scope="scope">
                        <el-button size="small" @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
                        <el-button size="small" type="danger" @click="handleDelete(scope.$index, scope.row)">删除</el-button>
                    </template>
                </el-table-column>
            </el-table>
        </div>

        <!-- 编辑弹出框 -->
        <el-dialog title="编辑" :visible.sync="editVisible" width="30%">
            <el-form ref="form" :model="tempArticleClass" label-width="100px">
                <el-form-item label="分类名称">
                    <el-input v-model="tempArticleClass.name"></el-input>
                </el-form-item>
            </el-form>
            <span slot="footer" class="dialog-footer">
                <el-button type="primary" @click="saveData">确 定</el-button>
                <el-button @click="editVisible = false">取 消</el-button>
            </span>
        </el-dialog>

        <!-- 删除提示框 -->
        <el-dialog title="提示" :visible.sync="delVisible" width="300px" center>
            <div class="del-dialog-cnt">删除不可恢复，是否确定删除？</div>
            <span slot="footer" class="dialog-footer">
                <el-button type="primary" @click="deleteRow">确 定</el-button>
                <el-button @click="delVisible = false">取 消</el-button>
            </span>
        </el-dialog>
    </div>
</template>

<script>
    export default {
        name: 'articleClass',
        components: { },
        data() {
            return {
                loading: true,
                articleClassList: [],
                delVisible: false,
                hackReset: false,
                editVisible: false,
                idx: -1,
                tempArticleClass: {
                }
            }
        },
        created() {
            this.getData();
        },
        methods: {
            // 分页导航
            handleCurrentChange(val) {
                this.cur_page = val;
                this.getData();
            },
            // 获取 easy-mock 的模拟数据
            getData() {
                this.loading =true
                this.$axios.get('/article/class/list').then((res) => {
                    this.articleClassList = res.data.model.reverse()
                    this.loading =false
                })
            },
            handleEdit(index, row) {
                this.idx = index;
                this.saveType = 'edit'
                this.hackReset =true
                this.tempArticleClass = JSON.parse(JSON.stringify(row))
                this.editVisible = true;
            },
            handleAdd() {
                this.saveType = 'add'
                this.tempArticleClass.id = null
                this.tempArticleClass.name = null
                this.hackReset =true
                this.editVisible = true
            },
            closeEdit(){
                this.hackReset = false //销毁组件
                this.editVisible = false
            },
            handleDelete(index, row) {
                this.idx = index;
                this.delVisible = true;
            },
            handleSelectionChange(val) {
                this.multipleSelection = val;
            },
            // 保存编辑
            saveData() {
                //将保存的信息提交
                this.$axios.post('/article/class/save',this.tempArticleClass).then((res) => {
                    if(res.data.success) {
                        if(this.saveType === 'edit'){
                            this.$set(this.articleClassList, this.idx, this.tempArticleClass);
                        }else {
                           this.articleClassList.unshift(this.tempArticleClass)
                        }
                        this.editVisible = false;
                        this.$message.success(`保存成功`);
                    }else{
                        this.$message.error(res.data.message);
                    }
                })
            },
            // 确定删除
            deleteRow(){
                let id = this.articleClassList[(this.idx)].id
                this.$axios.get('/article/class/del?id=' + id).then((res) => {
                   if(res.data.success){
                        this.articleClassList.splice(this.idx, 1);
                        this.$message.success('删除成功');
                    }else{
                        this.$message.error(res.data.message);
                    }
                })
                this.delVisible = false;
            }
        }
    }

</script>

<style scoped>
    .handle-box {
        margin-bottom: 20px;
    }

    .handle-select {
        width: 120px;
    }

    .handle-input {
        width: 300px;
        display: inline-block;
    }
    .del-dialog-cnt{
        font-size: 16px;
        text-align: center
    }
</style>
